package com.drug.mingkang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.drug.mingkang.mapper.ImmRepertoryMapper;
import com.drug.mingkang.model.ImmAppear;
import com.drug.mingkang.mapper.ImmAppearMapper;
import com.drug.mingkang.model.ImmRepertory;
import com.drug.mingkang.model.SellDetail;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.ImmAppearService;
import com.drug.mingkang.service.ImmRepertoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 入库/出库明细(ImmAppear)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-27 16:52:07
 */
@Service("immAppearService")
public class ImmAppearServiceImpl implements ImmAppearService {
    @Resource
    private ImmAppearMapper immAppearMapper;

    @Resource
    private ImmRepertoryService immRepertoryService;

    @Resource
    private HttpSession session;

    /**
     * 通过ID查询单条数据
     *
     * @param appearId 主键
     * @return 实例对象
     */
    @Override
    public ImmAppear queryById(String appearId) {
        return this.immAppearMapper.queryById(appearId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ImmAppear> queryAllByLimit(int offset, int limit) {
        return this.immAppearMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param immAppear 实例对象
     * @return 实例对象
     */
    @Override
    public ImmAppear insert(ImmAppear immAppear) {

//        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
//
//        List<ImmAppear> immAppearList = JSONObject.parseArray(wl,ImmAppear.class);
//
//        for(int i = 0; i < immAppearList.size(); i++){
//            ImmAppear immApp= immAppearList.get(i);
//            if(immApp.getAppearCount() != null){
//                ImmRepertory imma = immRepertoryService.updateOne(immApp,immName);
//                immApp.setRepertory(imma.getRepertoryId());
//                immApp.setStatus("2");
//                immApp.setOperator(perUsers.getUserName());
//            }
//        }

        this.immAppearMapper.insert(immAppear);
        return immAppear;
    }

    /**
     * 修改数据
     *
     * @param immAppear 实例对象
     * @return 实例对象
     */
    @Override
    public ImmAppear update(ImmAppear immAppear) {
        this.immAppearMapper.update(immAppear);
        return this.queryById(immAppear.getAppearId());
    }

    /**
     * 通过主键删除数据
     *
     * @param appearId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String appearId) {
        return this.immAppearMapper.deleteById(appearId) > 0;
    }


    /**
     * 新增
     * @param immAppear
     * @return
     */
    @Override
    public int insertSelective(ImmAppear immAppear) {
        return this.immAppearMapper.insertSelective(immAppear);
    }

    @Override
    public int insertBatch(List<ImmAppear> list) {
        for (ImmAppear ia:list) {
            immAppearMapper.deleteById(ia.getRepertory());
        }
        return this.immAppearMapper.insertBatch(list);
    }
}